package handler

import (
	"github.com/gin-gonic/gin"
	"trpg-base/entity"
	"trpg-common/request"
	"trpg-common/response"
)

type UserHandler struct{}

func (handler UserHandler) LogIn(c *gin.Context) {
	user := entity.User{}
	request.GetStructFromGinContext(c, &user)
	session := userService.LogIn(user)
	response.SuccessWithData(c, session)
}

func (handler UserHandler) Register(c *gin.Context) {
	user := entity.User{}
	request.GetStructFromGinContext(c, &user)
	session := userService.Register(user)
	response.SuccessWithData(c, session)
}

func (handler UserHandler) UpdateInfo(c *gin.Context) {
	user := entity.User{}
	request.GetStructFromGinContext(c, &user)
	session := c.GetHeader("authorization")
	userService.UpdateInfo(user, session)
	response.Success(c)
}

func (handler UserHandler) UploadAvatar(c *gin.Context) {
	userInfo := request.GetUserFromGinContext(c)
	file := request.GetFileFromForm(c, "file")
	response.SuccessWithData(c, userService.UploadAvatar(file, userInfo))
}

func (handler UserHandler) GetUserDetail(c *gin.Context) {
	user := request.GetUserFromGinContext(c)
	res := userService.GetUserByUid(user.Uid)
	response.SuccessWithData(c, res)
}
